import { ref } from "vue";
import { productGet } from "../api/home";
import { GoodsType } from "../types/prod";
export const useGoodsList = async () => {
  //商品列表
  const glist = ref<Array<GoodsType>>([]);
  const { data } = await productGet();
  glist.value = data.results;
  // console.log(data);

  //列表分页
  const finished = ref<boolean>(false);
  const loading = ref<boolean>(false);
  let page: number = 2;
  const onLoad = async () => {
    console.log("onLoad触发了");
    const { data } = await productGet(page);
    if (data.results.length) {
      page++;
      glist.value = [...glist.value, ...data.results];
      loading.value = false; //保证每次触底都能触发请求
      return;
    }
    finished.value = true; //没有更多数据了
  };
  return {
    glist,
    finished,
    loading,
    onLoad,
  };
};
